package cn.stylefeng.guns.modular.business.frequency.branchtenement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.CoreExceptionEnum;
import cn.stylefeng.guns.core.exception.CustomException;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.base.entity.BaseEntity;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.custom.constant.PublicCommon;
import cn.stylefeng.guns.modular.business.frequency.branchtenement.entity.BranchTenement;
import cn.stylefeng.guns.modular.business.frequency.branchtenement.mapper.BranchTenementMapper;
import cn.stylefeng.guns.modular.business.frequency.branchtenement.model.param.BranchTenementParam;
import cn.stylefeng.guns.modular.business.frequency.branchtenement.service.BranchTenementService;
import cn.stylefeng.guns.modular.business.frequency.branchtenement.enums.BranchTenementExceptionEnum;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租户网点表 服务实现类
 *
 * @author yujie
 * @date 2023/05/10 16:17
 */
@Service
public class BranchTenementServiceImpl extends ServiceImpl<BranchTenementMapper, BranchTenement> implements BranchTenementService {

    @Autowired
    private BusinessUtil businessUtil;
    @Override
    public ResponseData page(BranchTenementParam branchTenementParam) {



        Long tenantId = null;
        if (LoginContextHolder.me().isSuperAdmin()){
            tenantId = 1l;
        }else {
            tenantId = businessUtil.getTenantId();
        }
        branchTenementParam.setTenantId(tenantId);
        // 构造条件
        LambdaQueryWrapper<BranchTenement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BranchTenement::getTenantId,tenantId);
        queryWrapper.eq(branchTenementParam.getIsBranch()!=null,BranchTenement::getIsBranch,branchTenementParam.getIsBranch());
        queryWrapper.eq(BranchTenement::getDelFlag,PublicCommon.启用);
        queryWrapper.orderByDesc(BaseEntity::getCreateTime);
        Page<BranchTenement> page = this.page(PageFactory.defaultPage(), queryWrapper);

        // 查询分页结果
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(page,branchTenementParam));
    }

    @Override
    public List<BranchTenement> list(BranchTenementParam branchTenementParam) {

        Long tenantId = null;
        if (LoginContextHolder.me().isSuperAdmin()){
            tenantId = 1l;
        }else {
            tenantId = businessUtil.getTenantId();
        }
        branchTenementParam.setTenantId(tenantId);
        // 构造条件
        LambdaQueryWrapper<BranchTenement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BranchTenement::getTenantId,tenantId);
        queryWrapper.eq(branchTenementParam.getIsBranch()!=null,BranchTenement::getIsBranch,branchTenementParam.getIsBranch());
        queryWrapper.eq(BranchTenement::getDelFlag,PublicCommon.启用);

        return this.list(queryWrapper);
    }

    @Override
    public void add(BranchTenementParam branchTenementParam) {

        Long tenantId = null;
        if (LoginContextHolder.me().isSuperAdmin()){
            tenantId = 1l;
        }else {
            tenantId = businessUtil.getTenantId();
        }
        BranchTenement tenement = this.getOne(new QueryWrapper<BranchTenement>().lambda()
                .eq(BranchTenement::getBranchName, branchTenementParam.getBranchName())
                .eq(BranchTenement::getTenantId, tenantId)
                .eq(BranchTenement::getDelFlag, PublicCommon.启用));
        if(tenement!=null){
            throw new CustomException(CoreExceptionEnum.ERROR_PERMISSION.BRANCH_REPEAT.getMessage());
        }
        // 将dto转为实体
        BranchTenement branchTenement = new BranchTenement();
        BeanUtil.copyProperties(branchTenementParam, branchTenement);
        branchTenement.setTenantId(tenantId);

        this.save(branchTenement);
        //添加服务城市
        if (branchTenementParam.getChargeCodeList() != null && branchTenementParam.getChargeCodeList().size() > 0) {
            this.baseMapper.addSiteCity(branchTenement.getId(), branchTenementParam.getChargeCodeList());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BranchTenementParam branchTenementParam) {
        this.removeById(branchTenementParam.getId());
        //删除服务城市
        this.baseMapper.deleteCityCode(branchTenementParam.getId());
    }

    @Override
    public void edit(BranchTenementParam branchTenementParam) {

        Long tenantId = null;
        if (LoginContextHolder.me().isSuperAdmin()){
            tenantId = 1l;
        }else {
            tenantId = businessUtil.getTenantId();
        }
        BranchTenement tenement = this.getOne(new QueryWrapper<BranchTenement>().lambda()
                .eq(BranchTenement::getBranchName, branchTenementParam.getBranchName())
                .eq(BranchTenement::getTenantId, tenantId)
                .eq(BranchTenement::getDelFlag, PublicCommon.启用));
        if(tenement!=null){
            //判断是否名称没有修改
            BranchTenement branchTenement = this.getById(branchTenementParam.getId());
            if(!ObjectUtil.equal(branchTenement.getBranchName(),branchTenementParam.getBranchName())){
                throw new CustomException(CoreExceptionEnum.ERROR_PERMISSION.BRANCH_REPEAT.getMessage());
            }
        }
        // 根据id查询实体
        BranchTenement branchTenement = this.queryBranchTenement(branchTenementParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(branchTenementParam, branchTenement);

        this.updateById(branchTenement);

        //删除服务城市
        this.baseMapper.deleteCityCode(branchTenement.getId());
        //添加服务城市
        if (branchTenementParam.getChargeCodeList() != null && branchTenementParam.getChargeCodeList().size() > 0) {
            this.baseMapper.addSiteCity(branchTenement.getId(), branchTenementParam.getChargeCodeList());
        }

    }

    @Override
    public BranchTenement detail(BranchTenementParam branchTenementParam) {
        BranchTenement branchTenement = this.queryBranchTenement(branchTenementParam);
        branchTenement.setChargeCodeList(this.baseMapper.getServiceCodes(branchTenement.getId()));
        return branchTenement;
    }



    /**
     * 获取租户网点表
     *
     * @author yujie
     * @since 2023/05/10 16:17
     */
    private BranchTenement queryBranchTenement(BranchTenementParam branchTenementParam) {
        BranchTenement branchTenement = this.getById(branchTenementParam.getId());
        if (ObjectUtil.isEmpty(branchTenement)) {
            throw new ServiceException(BranchTenementExceptionEnum.NOT_EXISTED);
        }
        return branchTenement;
    }
}